﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace DeviceUnion.Common
{
    internal class ResouceHelper
    {
        public static string TTC { get; set; }
        public static string FFmpeg { get; set; }
        public static string Key_FFmpeg { get; set; }
        public static string Key_TTC { get; set; }
        public static string TempDir { get; set; }
        public static string CurDir { get; set; }

        static ResouceHelper()
        {
            TempDir = Path.GetTempPath();
            CurDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            Key_FFmpeg = "DeviceUnion.FFmpeg.ffmpeg.exe";
            Key_TTC = "DeviceUnion.FFmpeg.msyh.ttc";
            FFmpeg = "ffmpeg.exe";
            TTC = "msyh.ttc";
        }

        #region TOOLS

        public static void CreateSteamFile(string emName, string name, string dir)
        {
            if (File.Exists(Path.Combine(dir, name)))
                return;
            try
            {
                var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(emName);
                if (stream == null)
                {
                    return;
                }
                var writer = File.Create(Path.Combine(dir, name));
                CopyTo(stream, writer);
                writer.Close();
                stream.Close();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }
        }

        private static void CopyTo(Stream source, Stream destination)
        {
            byte[] array = new byte[81920];
            int count;
            while ((count = source.Read(array, 0, array.Length)) != 0)
            {
                destination.Write(array, 0, count);
            }
        }

        #endregion TOOLS

        public static void CreateFileAtCurrentDllDir()
        {
            CreateSteamFile(Key_TTC, TTC, CurDir);
            CreateSteamFile(Key_FFmpeg, FFmpeg, CurDir);
        }

        /// <summary>
        /// 打开帮助文档CHM
        /// </summary>
        public static void OpenHelperDoc(string chmFile)
        {
            var p = new Process();
            p.StartInfo.FileName = @"C:\Windows\hh.exe";
            p.StartInfo.Arguments = chmFile;
            p.StartInfo.UseShellExecute = false;
            p.Start();
        }
    }
}